/*
 * Licensed to Nanjing RedOrange Co. ltd (RedOrange) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * RedOrange licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package redora.rdo.generator;

/**
 * Archetype Template without content. Gives hints to the ModelProcessor what to
 * do with this template.
 * 
 * See also templates.xsd.
 * 
 * @author jaap
 */
public class Template {

	public Template(String name, String packageSuffix, Type type,
			Destination destination, DestinationType destinationType,
			Input input) {
		super();
		this.name = name;
		this.packageSuffix = packageSuffix;
		this.type = type;
		this.destination = destination;
		this.destinationType = destinationType;
		this.input = input;
	}

	public enum Type {
		freemarker, xslt, copy;
	}

	public enum Destination {
		source, target, webapp;
	}

	public enum DestinationType {
		java, xml, properties;
	}

	public enum Input {
		AllModels, Model, Language, Enum;
	}

	public String name;
	public String packageSuffix;
	public String ignoreProjects;

	public void setIgnoreProjects(String ignoreProjects) {
		this.ignoreProjects = ignoreProjects;
	}

	public Type type;
	public Destination destination;
	public DestinationType destinationType;
	public Input input;

	private String destinationFileName;
	private String templateFileName;

	public void setTemplateFileName(String templateFileName) {
		this.templateFileName = templateFileName;
	}

	public String getDestinationFileName(String objectName, String language,
			String classAttribute) {
		if (destinationFileName == null) {
			destinationFileName = name;
			if (objectName != null) {
				destinationFileName = destinationFileName.replace(
						"${objectName}", objectName);
			}
			if (language != null) {
				destinationFileName = destinationFileName.replace(
						"${language}", language);
			}
			if (classAttribute != null) {
				destinationFileName = destinationFileName.replace(
						"${classAttribute}", classAttribute);
			}

			if (destinationType != null) {
				destinationFileName = destinationFileName + "." + destinationType;
			}
		}
		return destinationFileName;
	}

	public String getTemplateFileName() {
		if (templateFileName == null) {
			templateFileName = name.replace("${objectName}", "Pojo");

			if (type == Type.freemarker) {
				templateFileName = templateFileName + ".ftl";
			} else if (type == Type.xslt) {
				templateFileName = templateFileName + ".xsl";
			}
		}
		return templateFileName;
	}
}
